using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ltf.Diffs;
using Ltf.Remoting.TFAccess;
using Ltf.Remoting.RemoteEvents;
using System.IO;

namespace Ltf.MonitorLizard.Forms
{
	[FormType(FormTypes.TwoWayDiff)]

	public partial class TwoWayDiff : Form
	{
		public enum DiffSide
		{
			Left,
			Right
		}

		public TwoWayDiff()
		{
			InitializeComponent();
		}


		private bool ignoreAllWhiteSpace = false;

		public TwoWayDiff(string[] pathList, LizardEventArgs args)
			:this ()
		{
				string pathLeft = pathList[0];
				string pathRight = pathList[1];

                if (string.IsNullOrEmpty(pathLeft) || string.IsNullOrEmpty(pathRight))
                    return;



				if (pathList.Length == 2)
					CompareSideBySide(pathList[0], pathList[1], ignoreAllWhiteSpace);
		}

		public TwoWayDiff(string path1, string path2)
			: this()
		{
				CompareSideBySide(path1, path2, ignoreAllWhiteSpace);

		}


		public string LeftFile;
		public string RightFile;

		string LeftFSFileName;
		string RightFSFileName;

		bool ignoreRefresh = false;
		bool sideBySide = true;

		
		private int contextLines=3;

		public void SetUp(string left, string right)
		{
			LeftFile = left;
			RightFile = right;
		}

		public void CompareSideBySide(string left, string right, bool ignoreAllWhiteSpace)
		{
            LeftFSFileName = left;
            RightFSFileName = right;

			tsblblInserts.Visible = true;
			tsblblDeletes.Visible = true;
			tsblblChanges.Visible = true;
			ignoreRefresh = true;
			SetUp(left, right);
			twoWayDiff1.SetCaptions(left, right);

			twoWayDiff1.ShowDiffSideBySide(LeftFSFileName, RightFSFileName, ignoreAllWhiteSpace);
			tsblblInserts.Text = string.Format("Inserts:{0}", twoWayDiff1.InsertsCount);
			tsblblDeletes.Text = string.Format("Deletes:{0}", twoWayDiff1.DeletesCount);
			tsblblChanges.Text = string.Format("Changes:{0}", twoWayDiff1.ChangesCount);
			ignoreRefresh = false;
		}

		public void CompareInline(string left, string right, bool ignoreAllWhiteSpace)
		{
			tsblblInserts.Visible = true;
			tsblblDeletes.Visible = true;
			tsblblChanges.Visible = true;
			ignoreRefresh = true;
			SetUp(left, right);
			twoWayDiff1.SetCaptions(left + "  :  " + right, string.Empty);

			twoWayDiff1.ShowDiffInLine(LeftFSFileName, RightFSFileName, contextLines, ignoreAllWhiteSpace);
			tsblblInserts.Text = string.Format("Inserts:{0}", twoWayDiff1.InsertsCount);
			tsblblDeletes.Text = string.Format("Deletes:{0}", twoWayDiff1.DeletesCount);
			tsblblChanges.Text = string.Format("Changes:{0}", twoWayDiff1.ChangesCount);
			ignoreRefresh = false;
		}

		

		private void toolStripButton2_Click(object sender, EventArgs e)
		{
			GotoPreviouschange();
		}

		private void GotoNextChange()
		{
			twoWayDiff1.Next();
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			GotoNextChange();
		}

		private void GotoPreviouschange()
		{
			twoWayDiff1.Previous();
		}

		private void tsbViewMode_Click(object sender, EventArgs e)
		{
			RefreshView();
		}

		private void RefreshView()
		{
			ignoreAllWhiteSpace = tsbIgnoreWhiteSpace.Checked;

			if (ignoreRefresh)
				return;
			if (sideBySide)
				CompareSideBySide(LeftFile, RightFile, ignoreAllWhiteSpace);
			else
				CompareInline(LeftFile, RightFile, ignoreAllWhiteSpace);
		}

		

		private void SetFilePickingMode(bool allowAny)
		{
			tsbOpenLeft.Enabled=allowAny;
		}

		private string ShowFileDialog(string filename)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			if (filename!=null && !filename.StartsWith("$"))
				ofd.FileName = filename;
			DialogResult dr = ofd.ShowDialog();
			if (dr == DialogResult.OK)
				return ofd.FileName;
			else
				return filename;
		}

		

		private void tsbOpenLeft_Click(object sender, EventArgs e)
		{
			string filename=string.Empty;
			if (LeftFile!=null && !LeftFile.StartsWith("$"))
				filename = LeftFile;
			LeftFile = ShowFileDialog(LeftFile);
			RefreshView();
		}

		private void tsbOpenRight_Click(object sender, EventArgs e)
		{
			string filename = string.Empty;
			if (RightFile!=null && !RightFile.StartsWith("$"))
				filename = LeftFile;
			RightFile = ShowFileDialog(LeftFile);
			RefreshView();
		}

		


		private void tstbContextLines_Leave(object sender, EventArgs e)
		{
			int old = contextLines;
			int cl;
			if (int.TryParse(tstbContextLines.Text,out cl))
				contextLines=cl;
			else if (tstbContextLines.Text==string.Empty)
				contextLines = -1;
			else
				tstbContextLines.Text=(contextLines==-1)?string.Empty:contextLines.ToString();

			if (old!=contextLines)
				RefreshView();
		}




		private void tsmShowXLines_Click(object sender, EventArgs e)
		{
			tsmShowAllLines.Checked = false;
			tsmShow3Lines.Checked = false;
			tsmShow5Lines.Checked = false;
			tsmShow7Lines.Checked = false;
			tsmShowCustomLines.Checked = false;

			ToolStripMenuItem tsmItem = (ToolStripMenuItem)sender;
			tsmItem.Checked = true;
			int cl;
			int.TryParse(tsmItem.Tag.ToString(),out cl);

			if (cl == -2)
				if (!int.TryParse(tstbContextLines.Text, out cl))
				{
					cl = 3;
					tstbContextLines.Text = "3";
				}

			if (cl != contextLines)
			{
				contextLines = cl;
				sideBySide = false;
				RefreshView();
			}

		}

		private void tsbViewSideBySide_Click(object sender, EventArgs e)
		{
			sideBySide = true;
			RefreshView();
		}

		private void tsbViewInline_ButtonClick(object sender, EventArgs e)
		{
			sideBySide = false;
			RefreshView();
		}

		string lastDialogFolder = string.Empty;
		private void tsbMergeDifferences_Click(object sender, EventArgs e)
		{
			string name=LeftFile;

			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Title = "Select Target File";
			if (lastDialogFolder != string.Empty)
			{
				ofd.FileName = Path.Combine(lastDialogFolder, name);
			}
			else
				ofd.FileName = name;
			
			DialogResult ofddr = ofd.ShowDialog();

			if ((ofddr==DialogResult.Yes || ofddr==DialogResult.OK)&& File.Exists(ofd.FileName))
			{
				lastDialogFolder = (Path.GetDirectoryName(ofd.FileName));

				string path1 = string.Empty;
				string path2 = string.Empty;
				ThreeWayDiff twd = new ThreeWayDiff(LeftFSFileName, RightFSFileName, ofd.FileName);
				twd.ShowDialog();
			}
		}

		private void tsbSwap_Click(object sender, EventArgs e)
		{

			string tmpLeft = LeftFile;
			LeftFile = RightFile;

			RightFile = tmpLeft;

			RefreshView();
		}




		private void tsbFind_Click(object sender, EventArgs e)
		{
			twoWayDiff1.FindNext(tsbTxtFind.Text);
		}

		private void tsbSearchPrev_Click(object sender, EventArgs e)
		{
			twoWayDiff1.FindPrevious(tsbTxtFind.Text);
		}

		private void tsbIgnoreWhiteSpace_Click(object sender, EventArgs e)
		{
			RefreshView();
		}

	}
}